package cn.dengta.webapp.user.entity;

import java.util.Date;
import javax.annotation.Nonnull;

import cn.dengta.common.model.*;
import cn.dengta.context.model.*;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.user.model.AuditStatus;
import cn.dengta.webapp.user.model.UserInfoSupply;
import cn.dengta.webapp.user.vo.VideoVO;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.*;

/**
 * 相册
 *
 * @author lgy
 * @since 2022-02-26
 */
@Getter
@Setter
@Accessors(chain = true)
public class Photo extends BizEntity implements BizFlag.WritableBizFlag {

	public static final int PHOTO_UPLOAD_MAX = 9;
	public static final int PRIVATE_PHOTO_UPLOAD_MAX = 30;
	public static final int VIDEO_UPLOAD_MAX = 1;
	/** 标识：审核复制标记 */
	public static final long BIZ_FLAG_AUDIT_COPY_MARK = 1L;

	/** 类型 0-图片 1-视频 */
	private BizType type;
	/** 用户ID */
	private Long userId;
	/** 图片地址 */
	private String images;
	/** 是否封面 */
	private Integer isCover;
	/** 审核用户ID */
	private Long verifyUserId;
	/** 审核结果 */
	private String verifyRemark;
	/** 排序 */
	private Integer sort;
	/**
	 * 聊郁新版才会有
	 *
	 * @see cn.dengta.webapp.user.model.UserInfoSupply
	 */
	private Integer tagIndex;
	/** 业务标识 */
	private long bizFlag;

	public transient Boolean privatePhotoView;

	public Photo init(Photo input, Date now) {
		this.type = X.expectNotNull(input.type, type);
		this.userId = X.expectNotNull(userId, input.userId);
		this.images = X.expectNotNull(input.images, images);
		this.isCover = X.expectNotNull(input.isCover, isCover, 0);
		this.verifyUserId = X.expectNotNull(input.verifyUserId, verifyUserId);
		this.verifyRemark = X.expectNotEmpty(input.verifyRemark, verifyRemark);
		this.sort = X.expectNotNull(input.sort, sort, 0);
		this.status = X.expectNotNull(status, input.status, AuditStatus.WAIT_VERIFY.value);
		this.state = X.expectNotNull(state, input.state, State.PUBLIC);
		this.bizFlag = X.expectNotNull(bizFlag, 0L);
		super.initTime(now);
		return this;
	}

	public boolean isDefault() {
		return Status.asResult(isCover);
	}

	public boolean forPhoto() {
		return BizType.PHOTO == type;
	}

	public VideoVO videoData() {
		return BizType.VIDEO == type && StringUtil.notEmpty(images) ? JSONUtil.parseObject(images, VideoVO.class) : null;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public boolean canShow() {
		return isEnabled() && getStatus() > AuditStatus.WAIT_VERIFY.value;
	}

	public boolean canDelete(@Nonnull Member user) {
		return isEnabled() && user.eqId(userId) && getStatus() >= AuditStatus.WAIT_VERIFY.value;
	}

	@Override
	public void setBizFlag(long flag) {
		bizFlag = flag;
	}

	public UserInfoSupply forSupply() {
		WesKit wesKit = WesKit.ofIdWesKit(this.userId);
		if (wesKit.isInterior()) {
			if (this.tagIndex == null) {
				return null;
			}
			return switch (this.type) {
				case PHOTO -> (Cmp.eq(this.tagIndex, 1) ? UserInfoSupply.PHOTO_TOUR : UserInfoSupply.PHOTO_LIVE);
				case VIDEO -> UserInfoSupply.PHOTO_VIDEO;
				default -> null;
			};
		} else {
			return switch (this.type) {
				case PHOTO -> UserInfoSupply.PHOTO;
				case VIDEO -> UserInfoSupply.PHOTO_VIDEO;
				default -> null;
			};
		}
	}

	@Getter
	public enum BizType implements LabelI18nProxy<BizType, Integer> {

		/** 图片 */
		PHOTO(0, UserEnumI18nKey.BIZ_TYPE_PHOTO),
		/** 视频 */
		VIDEO(1, UserEnumI18nKey.BIZ_TYPE_VIDEO),
		/** 私人相册 */
		PRIVATE_PHOTO(2, UserEnumI18nKey.BIZ_TYPE_PRIVATE_PHOTO),
		//
		;

		// 定义私有变量
		public final Integer value;
		final ValueProxy<BizType, Integer> proxy;

		// 构造函数，枚举类型只能为私有
		BizType(Integer value, String label) {
			this.value = value;
			this.proxy = new ValueProxy<>(this, value, label);
		}

		public static BizType[] getValues() {
			return ValueProxy.getCachedArray(BizType.class, BizType::values);
		}

		public static BizType of(Integer value) {
			return PHOTO.getValueOf(value);
		}
	}

	public static final String TYPE = "type";
	public static final String USER_ID = "user_id";
	public static final String IMAGES = "images";
	public static final String IS_COVER = "is_cover";
	public static final String VERIFY_USER_ID = "verify_user_id";
	public static final String VERIFY_REMARK = "verify_remark";
	public static final String SORT = "sort";
	public static final String TAG_INDEX = "tag_index";

}
